import { IpcEvent } from 'app/type'
import { ipcMain, IpcMainInvokeEvent, Menu, MenuItemConstructorOptions } from 'electron'

const menuOptions: MenuItemConstructorOptions[] = [
    {
        role: 'appMenu',
        submenu: [
            { role: 'about', label: '关于' },
            { role: 'hide', label: '隐藏' },
            { role: 'hideOthers', label: '隐藏其他应用' },
            { role: 'quit', label: '退出' },
        ],
    },
    {
        label: '窗口',
        submenu: [
            { role: 'zoomOut', label: '缩小' },
            { role: 'zoomIn', label: '放大' },
            { role: 'resetZoom', label: '恢复默认大小' },
            { type: 'separator' },
            { role: 'minimize', label: '最小化' },
            { role: 'zoom', label: '最大化' },
            { role: 'togglefullscreen', label: '切换全屏' },
            { type: 'separator' },
            { role: 'reload', label: '重新加载' },
            { role: 'toggleDevTools', label: '切换控制台' },
        ],
    },
]

export class AppMenu {
    static init() {
        AppMenu.steup(menuOptions)
        ipcMain.handle(
            IpcEvent.MENU_STEUP_APP,
            async (e: IpcMainInvokeEvent, options: MenuItemConstructorOptions[]) => {
                AppMenu.steup(options)
            },
        )
    }

    static steup(menuOptions: MenuItemConstructorOptions[]) {
        const menu = Menu.buildFromTemplate(menuOptions)
        Menu.setApplicationMenu(menu)
    }
}
